<template>
  <div>
    <el-form :inline="true">

    </el-form>
    <custom-table
      :data="allocateList"
      :columns="columns"
      :tableLoadingFlag="tableLoadingFlag"
      :operationColumn="operationColumn"
      :pagination="pagination">
    </custom-table>
  </div>
</template>

<script>
  import CustomTable from '@/components/table/CustomTable'
  import { apiGetPointAllotList } from '@/api/stock/index'
  import { parseTime } from '@/utils'
  import {getRoleType} from '@/utils/token'

  export default {
    components: {CustomTable},
    created () {
      let listQuery = {
        pageNumber: this.pagination.currentPage,
        pageSize: this.pagination.pageSize
      }
      this.getAllocateList(listQuery)
      this.def()
    },
    data () {
      return {
        tableLoadingFlag: false,          // 调拨列表
        columns: [
          {label: '接收人', name: 'receiverName', align: 'center'},
          {label: '接收省份', name: 'receiveProvince', align: 'center'},
          {label: '接收地址', name: 'receiverAddress', align: 'center'},
          {label: '型号', name: 'modelName', align: 'center'},
          {label: '数量', name: 'num', align: 'center'},
          {label: '欠单数量', name: 'accommodationNum', align: 'center'},
          {label: '要求发出时间', name: 'requiredDate', align: 'center', formatter: this.formatTime},
          {label: '状态', name: 'applicationStatus', align: 'center'}           // 待发货、已发货
        ],
        allocateList: [],
        roleType: getRoleType(),
        operationColumn: {
          width: '80',
          operations: [
            {name: '发货', callback: this.onBtnAllocate, page: 'allocate'}
          ]
        },
        pagination: this.getData(this.getAllocateList)
      }
    },
    methods: {
      getAllocateList (listQuery) {
        // TODO 调拨列表
        this.tableLoadingFlag = true
        apiGetPointAllotList(listQuery).then(response => {
          this.allocateList = response.data
          this.pagination.totalRecords = response.totalRecords
          this.tableLoadingFlag = false
        }).catch(() => {
          this.tableLoadingFlag = false
        })
      },
      onBtnAllocate (row) {
        // TODO 发货
        this.$router.push(
          {
            path: '/stock/logistic',
            query: {
              id: row.id,
              receiveProvince: row.receiveProvince,
              receiverAddress: row.receiverAddress,
              receiverName: row.receiverName,
              receiverTel: row.receiverTel,
              type: 3,
              modelName: row.modelName
            }
          })
      },
      formatTime (row, column, cellValue) {
        if (column.property === 'requiredDate') {
          return parseTime(cellValue, '{y}-{m}-{d}')
        } else {
          return parseTime(cellValue)
        }
      },
      def() {
        if (this.roleType !== '2' && this.roleType !== '3') {
          this.operationColumn = null
        }
      },
    }
  }
</script>
